/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.javalearningproject.VEK_ME.controllers;

import com.googlecode.javalearningproject.VEK_ME.GUI.MainGraphicsCanvas;
import javax.microedition.lcdui.Canvas;

    
/**
 *
 * @author Administrator
 */
public class MainControllerCanvas {
    
    private int screenWidth;
    private int screenHeight;
    private int totalWidth;
    private int totalHeight;
    
    
    public MainControllerCanvas(int screenW, int screenH, int tw, int th)
    {
        screenWidth = screenW;
        screenHeight = screenH;
        totalWidth = tw;
        totalHeight = th;
    }
    
    public void respondToInput(MainGraphicsCanvas mgc, int keycode, int currentPosX, int currentPosY)
    {
        System.out.println("before respondToInput: " + currentPosX + "," + currentPosY);
        System.out.println("keycode: " + keycode + "keys: " + MainGraphicsCanvas.KEY_NUM4
                + " and " + MainGraphicsCanvas.KEY_NUM6);
        
            if(keycode == MainGraphicsCanvas.KEY_NUM2) // NORTH
            {
                if(isAllowedToMoveNorth(currentPosX, currentPosY))
                {
                    this.moveScreenNorth(mgc, currentPosX, currentPosY);
                } 
            }
                
            else if(keycode == MainGraphicsCanvas.KEY_NUM4) // WEST
            {
                if(isAllowedToMoveWest(currentPosX, currentPosY))
                {
                    moveScreenWest(mgc, currentPosX, currentPosY);
                }
            }
                
            else if(keycode == MainGraphicsCanvas.KEY_NUM6) // EAST
            {
                if(isAllowedToMoveEast(currentPosX, currentPosY))
                {
                    moveScreenEast(mgc, currentPosX, currentPosY);
                }
            }
                
            else if(keycode == MainGraphicsCanvas.KEY_NUM8) // SOUTH
            {
                if(isAllowedToMoveSouth(currentPosX, currentPosY))
                {
                    moveScreenSouth(mgc, currentPosX, currentPosY);
                }
            }
                
    }
    
    private void moveScreenNorth(MainGraphicsCanvas mgc, int currentPosX, int currentPosY)
    {
        int pixelsToAddToCurrentPosition;
        
        if(currentPosY - screenHeight < 0)
        {
            pixelsToAddToCurrentPosition = currentPosY;
            
        }
        else
        {
            pixelsToAddToCurrentPosition = screenHeight;
        }
        mgc.moveView(currentPosX, currentPosY - pixelsToAddToCurrentPosition);
    }
    
    private void moveScreenWest(MainGraphicsCanvas mgc, int currentPosX, int currentPosY)
    {
        int pixelsToAddToCurrentPosition;
        
        if(currentPosX - screenWidth < 0)
        {
            pixelsToAddToCurrentPosition = currentPosX;
            
        }
        else
        {
            pixelsToAddToCurrentPosition = screenHeight;
        }
        mgc.moveView(currentPosX - pixelsToAddToCurrentPosition, currentPosY);
    }
    
    private void moveScreenEast(MainGraphicsCanvas mgc, int currentPosX, int currentPosY)
    {
        int pixelsToAddToCurrentPosition;
        
        if(currentPosX + screenWidth > totalWidth - screenWidth)
        {
            pixelsToAddToCurrentPosition = totalWidth - (currentPosX + screenWidth);
            
        }
        else
        {
            pixelsToAddToCurrentPosition = screenHeight;
        }
        mgc.moveView(currentPosX + pixelsToAddToCurrentPosition, currentPosY);
    }
    
    private void moveScreenSouth(MainGraphicsCanvas mgc, int currentPosX, int currentPosY)
    {
        int pixelsToAddToCurrentPosition;
        if(currentPosY + screenHeight > totalHeight - screenHeight)
        {
            pixelsToAddToCurrentPosition = totalHeight - (currentPosY + screenHeight);
            
        }
        else
        {
            pixelsToAddToCurrentPosition = screenHeight;
        }

        mgc.moveView(currentPosX, currentPosY + pixelsToAddToCurrentPosition);
    }
    
    private boolean isAllowedToMoveNorth(int x, int y)
    {
        if(y > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    private boolean isAllowedToMoveWest(int x, int y)
    {
        if(x > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    private boolean isAllowedToMoveEast(int x, int y)
    {
        if(x < totalWidth)
        {
            
            return true;
        }
        else
        {
            return false;
        }
    }
    
    private boolean isAllowedToMoveSouth(int x, int y)
    {
        if(y < totalHeight)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    

}
